package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.omg.CORBA.MARSHAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkspaceServiceImpl  implements WorkspaceService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    //--1、查询今日运营数据
    @Override
    public BusinessDataVO businessData(LocalDate now) {

        //--1、需要的数据
        Map paramMap=new HashMap();
        paramMap.put("time",now);
        Integer totalOrderCount=orderMapper.countByMap(paramMap);  //今天订单总数
        Integer newUsers=userMapper.countByMap(paramMap);          //今天新增用户数

        paramMap.put("status", Orders.COMPLETED);
        Double turnover=orderMapper.sumByMap(paramMap);            //营业额:今天完成订单的销售额
        Integer validOrderCount=orderMapper.countByMap(paramMap);  //有效订单数:今天完成的订单

        Double orderCompletionRate=0.0;               //订单完成率
        Double unitPrice=0.0;                         //平均客单价

        //--2、计算：【订单完成率】【平均客单价】，分母非null不为0
        if(totalOrderCount!=null && validOrderCount!=null && totalOrderCount!=0 && validOrderCount!=0){
            orderCompletionRate=validOrderCount.doubleValue()/totalOrderCount;
            unitPrice=turnover/validOrderCount;
        }

        //--2-1、double保留两位小数
        orderCompletionRate=new BigDecimal(orderCompletionRate)
                .setScale(2,BigDecimal.ROUND_UP).doubleValue();
        unitPrice=new BigDecimal(unitPrice).setScale(2,BigDecimal.ROUND_UP).doubleValue();

        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers).build();
    }


    //--2、查询订单管理数据
    @Override
    public OrderOverViewVO overviewOrders(LocalDate now) {

        //订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消
        Map paramMap=new HashMap();
        paramMap.put("time",now);
        Integer allOrders=orderMapper.countByMap(paramMap);    //全部订单:当前时间无状态

        paramMap.put("status",Orders.TO_BE_CONFIRMED);
        Integer waitingOrders=orderMapper.countByMap(paramMap); //待接单数量:status=2

        paramMap.put("status",Orders.CONFIRMED);
        Integer deliveredOrders=orderMapper.countByMap(paramMap);//待派送数量:status=3

        paramMap.put("status",Orders.COMPLETED);
        Integer completedOrders=orderMapper.countByMap(paramMap);//已完成数量:status=5

        paramMap.put("status",Orders.CANCELLED);
        Integer cancelledOrders=orderMapper.countByMap(paramMap); //已取消数量:status=6

        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders).build();
    }

    //--3、查询菜品总览
    @Override
    public DishOverViewVO overviewDishes(LocalDate now) {

        Map paramMap=new HashMap();
        paramMap.put("status",1);
        Integer sold=dishMapper.countByMap(paramMap);        // 已启售数量:status=1

        paramMap.put("status",0);
        Integer discontinued=dishMapper.countByMap(paramMap);// 已停售数量:status=0

        return DishOverViewVO.builder().discontinued(discontinued).sold(sold).build();
    }
    //--4、查询套餐总览
    @Override
    public SetmealOverViewVO overviewSetmeals(LocalDate now) {

        Map paramMap=new HashMap();
        paramMap.put("status",1);
        Integer sold=setmealMapper.countByMap(paramMap);        // 已启售数量:status=1

        paramMap.put("status",0);
        Integer discontinued=setmealMapper.countByMap(paramMap);// 已停售数量:status=0

        return SetmealOverViewVO.builder().discontinued(discontinued).sold(sold).build();
    }

    //Map集合键不能重复，相同key的值会覆盖掉


    public static void main(String[] args) {
        Map paramMap=new HashMap();
        paramMap.put("status",1);
        System.out.println(paramMap);
        paramMap.put("status",2);
        System.out.println(paramMap);
        paramMap.put("status1",2);
        System.out.println(paramMap);
    }
}
